<template>
  <div class="coupon-detail-container" :style="{background: `url('${bg}')`}">
    
    <!-- 卡券信息 -->
    <div class="coupon-card">
      <div class="coupon-header">
        <div class="logo">
          <img src="@/assets/img/logo.png" alt="工会logo">
        </div>
        <div class="coupon-info">
          <h2 class="coupon-title">{{ couponDetail.ticketName }}</h2>
          <p class="coupon-value" >价值{{ couponDetail.ticketsum }}元</p>
        </div>
      </div>

      <template v-if="qrStatus == 1">
        <div class="qrcode-container">
          <div class="qrcode">
            <img src="@/assets/img/qrcode-used.png" style="font-size: 12px;" alt="二维码">
          </div>
          <div class="coupon-code">
            <p>消费券码：<span>{{ ticketno }}</span></p>
          </div>
        </div>
      </template>
      <template v-else>
        <!-- 二维码区域 -->
        <div class="qrcode-container">
          <div class="qrcode">
            <img :src="qrCodeUrl" style="font-size: 12px;" alt="二维码">
          </div>
          <div class="coupon-code">
            <p>消费券码：<span>{{ ticketno }}</span></p>
          </div>
          <p class="refresh-btn" @click="refreshQrcode">
            <span class="refresh-icon">↻</span> 刷新二维码
          </p>
        </div>
        <div class="usage-info">
          <div class="info-item">
            <span class="label">使用有效期：</span>
            <span class="value">{{ couponDetail.validTime }}</span>
          </div>
          <div class="info-item">
            <span class="label">领取日期：</span>
            <span class="value">{{ formatDate(couponDetail.createtime) }}</span>
          </div>
          <div class="info-item usage-desc">
            <span class="label">使用说明：</span>
            <p class="value">{{ couponDetail.notes }}</p>
          </div>
        </div>
      </template>
    </div>
    <div class="coupon-card" v-if="qrStatus != 1" style="margin-top: 20px;">
      <div class="coupon-header" style="font-size: 16px;">合作商户</div>
      <ul class="hotel-list">
        <li v-for="item in hotelList" @click="handleTo(item)">
          <div class="img">
            <img :src="item.defaultImage" alt="">
            <span class="badge">查看详情</span>
          </div>
          <div>{{ item.custname }}</div>
        </li>
      </ul>
    </div>
    <div class="to-evaluate" v-if="qrStatus == 1">
      <div class="btn" @click="handleToEvaluate">
        去评价
      </div>
    </div>
  </div>
</template>

<script setup>
import bg from '@/assets/img/bg.png'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { couponApi, festivalApi } from '@/api'
const couponDetail = ref({})
const ticketno = ref('')
const qrCodeUrl = ref('')
const hotelList = ref([])
const qrStatus = ref()
const router = useRouter()


// 时间处理成 “年月日” 格式
const formatDate = (dateString) => {
  const date = new Date(dateString);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}年${month}月${day}日`;
}
const handleTo = (hotel) => {
  router.push(`/hotel?id=${hotel.id}`)
}



const init = () => {
  // 获取优惠券参数
  const router = useRouter()
  const { id } = router.currentRoute.value.query
  couponApi.getCouponDetail({id}).then(res => {
    couponDetail.value = res.data
    ticketno.value = res.data.ticketno
    qrStatus.value = res.data.isapply
    refreshQrcode()
  })

  const uid = localStorage.getItem('uid')
  festivalApi.getCompanyList({
    uid:uid,
    pageNum:1,
    pageSize:10000
  }).then(
    res => {
      if(res.code === 0) {
        hotelList.value = res.data
        hotelList.value.map(item => {
          item.defaultImage = window.APP_CONFIG?.apiBaseUrl + item.defaultImage
        })
      }
    }
  )
}

const refreshQrcode = () => {
  const uid = localStorage.getItem('uid')
  couponApi.generateQrCode({
    uid: uid,
    // ticketno: '5414222506' 
    ticketno: ticketno.value
  }).then(res => {
    qrCodeUrl.value = res.qrCodeUrl
  })

}
onMounted(() => {
  init()
})

const handleToEvaluate = () => {
  router.push({
    path: `/evaluate`,
    query: {
      id: couponDetail.value.id,
      name: couponDetail.value.ticketName
    }
  })
}
</script>

<style lang="less" scoped>
.coupon-detail-container {  
  min-height: 100vh;
  background-color: #f1f3f4;
  background-repeat: no-repeat;
  padding: 15px;
  display: flex;
  flex-direction: column;
  align-items: center;
  background-size: 100%;
}

.coupon-card {
  width: 100%;
  max-width: 400px;
  background-color: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding-bottom: 20px;
}

.coupon-header {
  background-color: #fff;
  padding: 15px;
  display: flex;
  align-items: center;
  border-bottom: 1px dashed #f3f3f3;
  
  .logo {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background-color: #ffeb3b;
    overflow: hidden;
    margin-right: 15px;
    display: flex;
    justify-content: center;
    align-items: center;
    
    img {
      width: 40px;
      height: 40px;
    }
  }
  
  .coupon-info {
    flex: 1;
    
    .coupon-title {
      font-size: 18px;
      font-weight: bold;
      margin: 0 0 5px 0;
    }
    
    .coupon-value {
      font-size: 14px;
      margin: 0;
      color: #FF6F1E;
    }
  }
}

.qrcode-container {
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  
  .qrcode {
    width: 200px;
    height: 200px;
    background-color: white;
    padding: 10px;
    border: 1px solid #eee;
    
    img {
      width: 100%;
      height: 100%;
    }
  }
  
  .refresh-btn {
    margin: 0 0 15px 0;
    color: #f15a4a;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    
    .refresh-icon {
      font-size: 16px;
      margin-right: 5px;
    }
  }
}

.coupon-code {
  text-align: center;
  padding: 0 20px;
  
  p {
    font-size: 14px;
    color: #333;
    
    span {
      font-weight: bold;
    }
  }
}

.usage-info {
  padding: 0 15px;
  border-top: 1px dashed #f3f3f3;
  padding-top: 15px;
  
  .info-item {
    margin-bottom: 10px;
    font-size: 14px;
    display: flex;
    
    .label {
      color: #333;
      width: 90px;
      flex-shrink: 0;
    }
    
    .value {
      color: #999;
      flex: 1;
    }
    
    &.usage-desc {
      display: block;
      
      .label {
        display: block;
        margin-bottom: 5px;
      }
      
      .value {
        line-height: 1.5;
        margin: 0;
      }
    }
  }
}

.hotel-list{
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 10px;
  li{
    width: 48%;
    margin-bottom: 10px;
    div.img{
      position: relative;
      .badge {
        position: absolute;
        right: 0px;
        bottom: 0px;
        background: #FCDC5A;
        color: #008046;
        font-size: 10px;
        padding: 1px 4px;
        border-radius: 8px 0 8px 0;
        pointer-events: none;
      }
    }
    img{
      width: 100%;
      border-radius: 8px;
      height: 95px;
      display: block;
    }
    div{
      text-align: center;
      font-size: 12px;
      color: #333;
    }
  }
}

.to-evaluate{
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 10px;
  background: #fff;
  .btn{
    box-sizing: border-box;
    height: 40px;
    background: #EE5143;
    border-radius: 22px;
    font-size: 14px;
    text-align: center;
    color: #fff;
    line-height: 40px;
    width: calc(100% - 20px);
  }
}
</style>